package handler

import (
	"fmt"
	"go.getaway.adminspro.padmins.com/untils"
	"go.getaway.adminspro.padmins.com/untils/auth"
	"go.getaway.adminspro.padmins.com/untils/businessCommonQuery"
	"go.getaway.adminspro.padmins.com/untils/db"
	"go.getaway.adminspro.padmins.com/untils/di"
	"go.getaway.adminspro.padmins.com/untils/request"
	"go.getaway.adminspro.padmins.com/untils/response"
	"strings"
)

type Handler struct {
	Di                  *di.Di
	Db                  *db.Db
	Response            response.ResponseInterface
	Request             request.RequestInterface
	Auth                *auth.Auth
	BusinessCommonQuery *businessCommonQuery.BusinessCommonQuery
}

func (c Handler) ListFormat(list map[string]interface{}, row map[string]interface{}) {
	for key, item := range list {
		if val, ok := row[key]; ok {
			row[key+"_text"] = ""
			values := []interface{}{}
			for _, value := range strings.Split(fmt.Sprintf("%v", val), ",") {
				values = append(values, value)
			}

			for k, v := range item.(map[string]interface{}) {
				if untils.InArray(k, values) {
					row[key+"_text"] = fmt.Sprintf("%s%s,", row[key+"_text"], v)
				}
			}

			if row[key+"_text"] != "" {
				row[key+"_text"] = strings.Trim(row[key+"_text"].(string), ",")
			}
		}
	}
}
